package com.huatai.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.entity.SysOperLog;
import com.huatai.common.util.PageUtils;
import com.huatai.common.util.StringUtil;
import com.huatai.user.dto.LogIdsDto;
import com.huatai.user.entity.Department;
import com.huatai.user.entity.User;
import com.huatai.user.mapper.SysOperLogMapper;
import com.huatai.user.service.DepartmentService;
import com.huatai.user.service.ISysOperLogService;
import com.huatai.user.vo.LogParamInfo;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 操作日志 服务层处理
 *
 * @author ZSJ
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService {
	@Autowired
	private SysOperLogMapper operLogMapper;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private UserServiceImpl userService;

	/**
	 * 新增操作日志
	 *
	 * @param operLog 操作日志对象
	 * @return 结果
	 */
	@Override
	public boolean insertOperlog(SysOperLog operLog) {
		//设置部门
		LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
		//根据平台账号查询
		queryWrapper.eq(User::getRealName, operLog.getOperName());
		User user = userService.getOne(queryWrapper);
		if (user != null && user.getDeptId() != null) {
			Department department = departmentService.getById(user.getDeptId());
			operLog.setDeptName(department.getDeptName());
		}
		return save(operLog);
	}

	/**
	 * 查询系统操作日志集合
	 *
	 * @param logParamInfo 操作日志对象
	 * @return 操作日志集合
	 */
	@Override
	public PageUtils selectOperLogListByPage(LogParamInfo logParamInfo) {
		LambdaQueryWrapper<SysOperLog> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(StringUtil.isNotBlank(logParamInfo.getOperName()), SysOperLog::getOperName, logParamInfo.getOperName());
		if (logParamInfo.getStatus() != null) {
			queryWrapper.eq(SysOperLog::getStatus, logParamInfo.getStatus());
		}
		queryWrapper.like(StringUtil.isNotBlank(logParamInfo.getTitle()), SysOperLog::getTitle, logParamInfo.getTitle());
		queryWrapper.orderByDesc(SysOperLog::getOperTime);
		IPage<SysOperLog> sysOperLogIPage = new Page<>(logParamInfo.getPage(), logParamInfo.getSize());
		return new PageUtils(this.page(sysOperLogIPage, queryWrapper));
	}

	/**
	 * 查询系统操作日志集合
	 *
	 * @return 操作日志集合
	 */
	@Override
	public List<SysOperLog> selectLogList(LogIdsDto logIdsDto) {
		List<SysOperLog> sysOperLogList = Lists.newArrayList();
		for (Long operId : logIdsDto.getOperIds()) {
			sysOperLogList.add(operLogMapper.selectOperLogById(operId));
		}
		return sysOperLogList;
	}

	/**
	 * 批量删除系统操作日志
	 *
	 * @param operIds 需要删除的操作日志ID
	 * @return 结果
	 */
	@Override
	public int deleteOperLogByIds(Long[] operIds) {
		return operLogMapper.deleteOperLogByIds(operIds);
	}

	/**
	 * 查询操作日志详细
	 *
	 * @param operId 操作ID
	 * @return 操作日志对象
	 */
	@Override
	public SysOperLog selectOperLogById(Long operId) {
		return operLogMapper.selectOperLogById(operId);
	}

	/**
	 * 清空操作日志
	 */
	@Override
	public void cleanOperLog() {
		operLogMapper.cleanOperLog();
	}
}
